package com.dys.filter;

import javax.servlet.*;
import java.io.IOException;

/**
 * @Author: 疾风
 * @ClassName: Filter0_EncodingFilter
 * @Description: 设置编码字符集的过滤器
 * @CreateDate: 2024/09/25
 * @Version: v1.0.0
 */
public class Filter0_EncodingFilter implements Filter {
    private String charset;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("执行Filter0_EncodingFilter的init()方法......");
        charset = filterConfig.getInitParameter("charset");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 设置请求的编码方式
        servletRequest.setCharacterEncoding(charset);
        System.out.println("Filter0_EncodingFilter 正在执行请求过滤");
        // 设置响应内容类型和字符集
        servletResponse.setContentType("text/html;charset=utf-8");
        // 放行
        filterChain.doFilter(servletRequest, servletResponse);
        System.out.println("Filter0_EncodingFilter 正在执行响应过滤");
    }

    @Override
    public void destroy() {
        System.out.println("执行Filter0_EncodingFilter的 destroy()方法......");
    }
}
